<?php

namespace App\Admin\Controllers;

use App\Models\Gym;
use App\Models\GymTicket;
use App\Models\UserTicket;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class UserTicketController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('健身券领取');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
//    public function edit($id)
//    {
//        return Admin::content(function (Content $content) use ($id) {
//
//            $content->header('header');
//            $content->description('description');
//
//            $content->body($this->form()->edit($id));
//        });
//    }

    /**
     * Create interface.
     *
     * @return Content
     */
//    public function create()
//    {
//        return Admin::content(function (Content $content) {
//
//            $content->header('header');
//            $content->description('description');
//
//            $content->body($this->form());
//        });
//    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserTicket::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->id('ID')->sortable();
            $grid->user()->mobile("用户手机号");
            $grid->ticket()->gym_id('所属健身房')->display(function ($gym_id) {
                return Gym::where('id', $gym_id)->value('name');
            });
            $grid->ticket()->name('健身券名称');
            $grid->get_time("领取时间")->display(function ($get_time) {
                return date("Y-m-d H:i:s", $get_time);
            });
            $grid->company('公司名称');
            $grid->position('职位');
            $grid->wechat_no('微信号');
            $grid->tickets_count('健身券发放数量');
            $grid->status('审核状态')->display(function ($status) {
               if ($status == 1) {
                   return "<span class=\"label label-primary\">未审核</span>";
               } elseif ($status == 2) {
                   return "<span class=\"label label-success\">审核通过</span>";
               } else {
                   return "<span class=\"label label-danger\">审核未通过</span>";
               }
            });
            $grid->disableCreateButton();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                // append一个操作
                $actions->append('<a href="/admin/user-ticket/' . $actions->getKey() .'/check"><i class="fa fa-eye">审核</i></a>');
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                $filter->like('user.mobile', '用户手机号')->mobile();
                $filter->like('ticket.name', '健身券名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserTicket::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->hidden('id');
            $form->hidden('ticket_id');
            $form->display('user.mobile', '用户手机');
            $form->display('ticket.gym_id', '所属健身房')->with(function ($value) {
                return Gym::where('id', $value)->value('name');
            });
            $form->display('ticket.name', '优惠券名称');
            $form->display('get_time', '领取时间')->with(function ($value){
                return date("Y-m-d H:i:s", $value);
            });
            $form->display('company', '公司名称');
            $form->display('position', '职位');
            $form->display('wechat_no', '微信号');
            $form->select('status', '是否审核通过')->options([
                2 => '审核通过',
                3 => '审核不通过'
            ])->rules('required', [
                'required' => '请选择审核通过与否'
            ]);
            $form->number('tickets_count', '发放优惠券数量');
            $form->saving(function (Form $form) {

//                if ($form->status == 2 ) {
                    if ($form->tickets_count < 0) {
                        $error = new MessageBag([
                            'title' => '错误',
                            'message' => '优惠券发放数量不能小于0',
                        ]);
                        return back()->with(compact('error'));
                    }
                    $user_tickets = UserTicket::where('id', $form->id)->first();
                    $ticket = GymTicket::where('id', $form->ticket_id)->first();

                    $diff_count = $form->tickets_count - $user_tickets->tickets_count;
                    if ($ticket['remain_count'] < $diff_count) {
                        $error = new MessageBag([
                            'title' => '错误',
                            'message' => '优惠券发放数量超过剩余数量',
                        ]);
                        return back()->with(compact('error'));
                    }

                    GymTicket::where('id', $form->ticket_id)->decrement('remain_count', $diff_count);

//                }
            });

        });
    }


    /**
     * 审核
     *
     * @param $id
     * @return Content
     */
    public function check($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('报名');
            $content->description('审核');

            $content->body($this->form()->edit($id));
        });
    }
}
